package com.xunwulian.dubbo.service.impl;

import com.xunwulian.api.dto.BaseResponse;
import com.xunwulian.api.iface.CardAPI;
import com.xunwulian.base.dao.CardInfoDao;
import com.xunwulian.base.entity.CardInfoEntity;
import com.xunwulian.base.entity.Page;
import com.xunwulian.base.entity.User;
import com.xunwulian.dubbo.service.CardFileServer;
import com.xunwulian.dubbo.service.InnerCardInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */
@Service
public class CardImpl implements CardAPI {

    @Autowired
    private CardFileServer cardFileServer;

    @Autowired
    private CardInfoDao cardInfoDao;

    @Autowired
    private InnerCardInfoService innerCardInfoService;

    public CardImpl() {
    }

    @Override
    public BaseResponse parse2db(String file, String appId, User user) {

        // 解析文件
        new Thread(() -> {
            // TODO 异常处理
            cardFileServer.process2db(file, appId, user);
        }).start();
        
        return new BaseResponse();
    }

    @Override
    public BaseResponse addMsisdn(String msisdn, String appId, User user) {
        innerCardInfoService.addMsisdn(msisdn, appId, user);
        return new BaseResponse();
    }

    @Override
    public BaseResponse parseGprsSms2db(String file) {
        // TODO 异常处理
        cardFileServer.parseGprsSms2db(file);
        return new BaseResponse();
    }



    @Override
    public CardInfoEntity getByMsisdn(@Param("msisdn") String msisdn) {
        return cardInfoDao.getByMsisdn(msisdn);
    }

    @Override
    public BaseResponse listPage(Page page, String appId, String userId, String msisdnKey, String userNameKey) {

        BaseResponse<List<CardInfoEntity>> response = new BaseResponse<>();

        List<CardInfoEntity> cardList = cardInfoDao.listPage(page, appId, userId, msisdnKey, userNameKey);

        response.setData(cardList);
        response.setPage(page);

        return response;
    }
}
